//
//  ChainObjects.m
//  Charles
//
//  Created by Leo on 2019/7/1.
//  Copyright © 2019 charles. All rights reserved.
//

#import "ChainObjects.h"

@interface Trouble ()

@property (nonatomic, assign) NSInteger number; // 问题编号

@end

@implementation Trouble

- (instancetype)initWithNumber:(NSInteger)number {
    if (self = [super init]) {
        self.number = number;
    }
    return self;
}

- (NSInteger)number {
    return _number;
}

- (NSString *)toString {
    return [NSString stringWithFormat:@"Trobule %d",(int)self.number];
}

@end

@interface Support ()

@property (nonatomic, strong) NSString *name;

@end

@implementation Support

- (instancetype)initWithName:(NSString *)name {
    if (self = [super init]) {
        self.name = name;
    }
    return self;
}

- (void)support:(Trouble *)trouble {
    if ([self resolve:trouble]) {
        [self done:trouble];
    }else if (self.next != nil) {
        [self.next support:trouble];
    }else {
        [self fail:trouble];
    }
}

- (BOOL)resolve:(Trouble *)trouble {
    NSLog(@"%@ cannot resolve go to next",self.name);
    return NO;
}

- (void)done:(Trouble *)trouble {
    NSLog(@"%@ is resolved by %@",trouble.toString, self.name);
}

- (void)fail:(Trouble *)trouble {
    NSLog(@"%@ cannot be resolved.",trouble.toString);
}

- (NSString *)toString {
    return self.name;
}

@end

@implementation NoSupport

- (instancetype)initWithName:(NSString *)name {
    if (self = [super initWithName:name]) {
    
    }
    return self;
}

- (BOOL)resolve:(Trouble *)trouble {
    NSLog(@"%@ cannot resolve go to next",self.name);
    return NO;
}

@end

@interface LimitSupport ()

@property (nonatomic, assign) NSInteger limit;

@end

@implementation LimitSupport

- (instancetype)initWithName:(NSString *)name limit:(NSInteger)limit {
    if (self = [super initWithName:name]) {
        self.limit = limit;
    }
    return self;
}

- (BOOL)resolve:(Trouble *)trouble {
    if (trouble.number < self.limit) {
        return YES;
    }
    NSLog(@"%@ cannot resolve go to next",self.name);
    return NO;
}

@end

@implementation OddSupport

- (instancetype)initWithName:(NSString *)name{
    if (self = [super initWithName:name]) {
    }
    return self;
}

- (BOOL)resolve:(Trouble *)trouble {
    if (trouble.number % 2 == 1) {
        return YES;
    }
    NSLog(@"%@ cannot resolve go to next",self.name);
    return NO;
}

@end

@interface SpecialSupport ()

@property (nonatomic, assign) NSInteger number;

@end

@implementation SpecialSupport

- (instancetype)initWithName:(NSString *)name number:(NSInteger)number {
    if (self = [super initWithName:name]) {
        self.number = number;
    }
    return self;
}

- (BOOL)resolve:(Trouble *)trouble {
    if (trouble.number == self.number) {
        return YES;
    }
    NSLog(@"%@ cannot resolve go to next",self.name);
    return NO;
}

@end
